using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;
using System.Collections.Generic;
using System.Globalization;

public partial class Pages_AManageProduct : Mgh.UI.Sql.PageBase
{
    #region properties

    protected CultureInfo myInfo;
    private ProductBLL bn;

    protected string PageIndex
    {
        get { return Request.QueryString["pin"] == null ? "0" : Request.QueryString["pin"]; }
    }

    protected string CategoryID
    {
        get 
        {
            if (string.IsNullOrEmpty(Request.QueryString["cty"]))
                return "ALL";
            else if (Request.QueryString["cty"].ToUpper().Equals("ALL"))
                return "ALL";
            return Request.QueryString["cty"];
        }
    }

    protected string Status
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["sts"]))
                return "ALL";
            else if (Request.QueryString["sts"].ToUpper().Equals("ALL"))
                return "ALL";
            return Request.QueryString["sts"];
        }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        myInfo = CultureInfo.InvariantCulture;
        if (!IsPostBack)
        {
            this.LoadCategory();
            dlCate.SelectedValue = this.CategoryID;
            dlStatus.SelectedValue = this.Status;
            //this.LoadFeature();
            gv.PageIndex = int.Parse(this.PageIndex);
        }
    }

    protected void odsCreating(object sender, ObjectDataSourceEventArgs e)
    {
        bn = new ProductBLL(getCurrentConnect());
        e.ObjectInstance = bn;
    }

    protected void odsSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (!e.ExecutingSelectCount)
        {
            List<object> list = new List<object>();
            e.InputParameters["listFeature"] = list;
            e.InputParameters["strCate"] = dlCate.SelectedValue;
            e.InputParameters["strStatus"] = dlStatus.SelectedValue;
        }
            
        //foreach (ListItem item in cblFeature.Items)
        //{
        //    if (item.Selected)
        //        list.Add(item.Value);
        //}
        
    }

    protected void odsSelected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue == null)
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    protected void clickAdd(object sender, EventArgs e)
    {
        Response.Redirect("AEditProduct.aspx?pid=&pin=" + gv.PageIndex + "&cty=" + dlCate.SelectedValue + "&sts=" + dlStatus.SelectedValue);
    }

    protected void clickDisable(object sender, EventArgs e)
    {
        HtmlInputCheckBox ck = new HtmlInputCheckBox();
        List<object> lis = new List<object>();
        ProductBLL bn = new ProductBLL(getCurrentConnect());
        foreach (GridViewRow row in gv.Rows)
        {
            ck = row.FindControl("ckMember") as HtmlInputCheckBox;
            if (ck.Checked)
            {
                lis.Add(gv.DataKeys[row.DataItemIndex].Value.ToString());
            }
        }
        if (bn.disableProducts(lis))
        {
            gv.DataBind();
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    protected void clickEnable(object sender, EventArgs e)
    {
        HtmlInputCheckBox ck = new HtmlInputCheckBox();
        List<object> lis = new List<object>();
        ProductBLL bn = new ProductBLL(getCurrentConnect());
        foreach (GridViewRow row in gv.Rows)
        {
            ck = row.FindControl("ckMember") as HtmlInputCheckBox;
            if (ck.Checked)
            {
                lis.Add(gv.DataKeys[row.DataItemIndex].Value.ToString());
            }
        }
        if (bn.enableProducts(lis))
        {
            gv.DataBind();
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    protected void btFilter_Click(object sender, EventArgs e)
    {
        gv.DataBind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbHinh = e.Row.FindControl("lbHinh") as Label;
            Image imgHinh = e.Row.FindControl("imgHinh") as Image;
            if (lbHinh != null && imgHinh != null)
            {
                if (lbHinh.Text.Length == 0)
                    imgHinh.Attributes.Add("style", "opacity:0.3");
                else if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(bn.getImgURL(lbHinh.Text))))
                    imgHinh.Attributes.Add("style", "opacity:0.3");
            }

            Label lbStatus = e.Row.FindControl("lbStatus") as Label;
            Image imgStatus = e.Row.FindControl("imgStatus") as Image;
            if (lbStatus != null && imgStatus != null)
            {
                if (lbStatus.Text.ToLower().Equals("disable"))
                    imgStatus.Attributes.Add("style", "opacity:0.3");
            }

        }
    }

    #endregion

    #region functions

    private void LoadCategory()
    {
        CategoryBLL bn = new CategoryBLL(getCurrentConnect());
        DataSources.Jw_Tbl_CategoryDataTable tb = bn.getCategories();
        if (tb != null)
        {
            dlCate.DataSource = tb;
            dlCate.DataTextField = tb.TenColumn.ColumnName;
            dlCate.DataValueField = tb.CateIDColumn.ColumnName;
            dlCate.DataBind();
            dlCate.Items.Insert(0, new ListItem("All", "ALL"));
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }
    }

    private void LoadFeature()
    {
        //ProductBLL bn = new ProductBLL(getCurrentConnect());
        //DataSources.Jw_Tbl_FeatureDataTable tb = bn.getFeatures();
        //if (tb != null)
        //{
        //    cblFeature.DataSource = tb;
        //    cblFeature.DataTextField = tb.TenColumn.ColumnName;
        //    cblFeature.DataValueField = tb.FeaIDColumn.ColumnName;
        //    cblFeature.DataBind();
        //}
        //else
        //{
        //    cval.IsValid = false;
        //    cval.Text = bn.getMessage();
        //}
    }

    protected bool checkIsEnable(object obj)
    {
        if (obj != null)
        {
            if (obj.ToString().ToLower().Equals("enable"))
            {
                return true;
            }
        }
        return false;
    }

    protected bool checkIsImage(object obj)
    {
        if (obj != null)
        {
            if (obj.ToString().Length > 0)
            {
                return true;
            }
        }
        return false;
    }

    #endregion

    
}
